/** @file caLog.h
 * Defines and prototypes that deal with logging for ConArtist
 *
 */

/*
 * Licensed to the ConArtist Development Team (CADT).
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 * The CADT licenses this file to you under the Apache License, 
 * Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef CALOG_H
#define CALOG_H

/** @defgroup LOGGING ConArtist Logging
 * Conartist allows multiple logging facilities to be defined.
 * When log messages are written they are written to all facilities
 * with a log level at or below the level defined in the call to log.
 * @{
 */

#define CA_LOG_FATAL    1  /**< Fatal error */
#define CA_LOG_ERROR    2  /**< Error */
#define CA_LOG_WARN     3  /**< Warning */
#define CA_LOG_INFO     4  /**< Informational message */
#define CA_LOG_DEBUG    5  /**< Debug message */

#ifdef LOG_NAMES
char *levelNames[] = {
    "None", "FATAL", "ERROR", "WARN", "INFO", "DEBUG", NULL
};
#endif

/** @fn void caLog(int lvl, const char *fmt, ...)
 * Write a message to the log.
 * The message gets sent to all the defined logging facilities.
 * @param lvl CA_LOG_ define
 * @param fmt The message format
 */
void caLog(int, const char *, ...);

/** The caLogFacility structure defines a single facility that logs
 * messages at the given log level.
 * The structures are allocated from the @a globalPool.
 */
struct caLogFacility {
    struct caLogFacility *next;  /**< pointer to next facility */
    /** name used for logging facility (used to reference facility later) */
    char *name;
    int level;         /**< level of logging, one of the CA_LOG constants */
    /** function to write message to logging facility */
    apr_status_t (*write)(void *, int, const char *);
    /** function to close the logging facility */
    void (*close)(void *);
    void *data;        /**< facility specific data */
};

/** @fn struct caLogFacility *caOpenSyslog(const char *facility, 
                                           const char *name)
 * Open a syslog logging facility.
 * @param facility Name of the syslog facility to open
 * @param name     Internal name to assign the log facility
 */
struct caLogFacility *caOpenSyslog(const char *, const char *);

/** @fn struct caLogFacility *caOpenLogFile(const char *path,
 *                                          const char *name)
 * Open a file to log to.
 * @param path Full path to the file to be opened.
 * @param name Internal name to assign the log facility
 */
struct caLogFacility *caOpenLogFile(const char *, const char *);

/** @fn void caLogClose(struct caLogFacility *cf)
 * Close the log facility. This also removes it from the server if it
 * is registered.
 * @param cf Pointer to log facility structure
 */
void caLogClose(struct caLogFacility *cf);

/** @fn apr_status_t addLogFacility(struct caLogFacility *f)
 * Add a created log facility to the server. Once added it will be used
 * for all subsequent calls.
 * @param f Pointer to created caLogFacility structure
 * @return Returns APR_SUCCESS if succesfully added.
 */
apr_status_t addLogFacility(struct caLogFacility *);

/** @fn int caLogLevelString(const char *level)
 * Returns the appropriate log level of the supplied string.
 * @param level Pointer to the string to containing the log level
 */
int caLogLevelString(const char *);

/** @} */

#endif /* CALOG_H */
